package com.jinglin.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jinglin.common.UserThreadLocal;
import com.jinglin.exception.ProjectException;
import com.jinglin.pojo.User;
import com.jinglin.service.UserService;
import com.jinglin.utils.JwtUtil;
import com.jinglin.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;

@RestController
public class userController {
    @Autowired
    private UserService userService;
    // 注册用户
    @PostMapping("/api/reg")
    public Result reg(@RequestBody User user){
        boolean save = userService.save(user);
        return Result.success(null);
    }
    // 用户登录
    @PostMapping("/api/login")
    public Result login(@RequestBody User user) throws Exception {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername,user.getUsername());
        wrapper.eq(User::getPassword,user.getPassword());
        User one = userService.getOne(wrapper);
        if (one == null ){
            throw  new ProjectException("用户名密码错误！！");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("username", one.getUsername());
        map.put("password", one.getPassword());
        if (one.getId() != null) {
            String token = JwtUtil.createJWT("jinglin", 60 * 60 * 1000, map);
            return Result.success(token);
        } else {
            return Result.fail("用户名密码错误！！！");
        }
    }
    // 获取用户信息
    @GetMapping("/my/userinfo")
    public Result getUserInfo(){
        User user = UserThreadLocal.getUser();
        String username = user.getUsername();
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername,username);
        User one = userService.getOne(wrapper);
        System.out.println(one);
        return Result.success(one);
    }
    // 更新用户资料
    @PutMapping("/my/userinfo")
    public Result updateUserInfo(@RequestBody User user) {
        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(User::getId, user.getId());
        userService.update(user,wrapper);
        return Result.success(null);
    }
    // 新增用户头像
    @PatchMapping("/my/update/avatar")
    public Result updateUserAvatar(@RequestBody Map<Object, Object> map) {
        System.out.println(map);
        // 获取拦截器那边传进来的username
        User userInfo = UserThreadLocal.getUser();
        String username = userInfo.getUsername();
        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(User::getUsername,username);
        wrapper.set(User::getUser_pic,map.get("avatar"));
        userService.update(wrapper);
        return Result.success("新增用户头像成功！！");
    }
    // 重置密码
    @PatchMapping("/my/updatepwd")
    public Result updatePwd(@RequestBody Map<String,String> map) throws Exception {
        User user = UserThreadLocal.getUser();
        String old_pwd = map.get("old_pwd");
        String new_pwd = map.get("new_pwd");
        String re_pwd = map.get("re_pwd");
        if (user.getPassword().equals(old_pwd)){
            // 如果密码验证通过，则进行修改
            if (new_pwd.equals(re_pwd)){
                LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
                wrapper.eq(User::getUsername, user.getUsername());
                wrapper.set(User::getPassword, new_pwd);
                userService.update(wrapper);
                return Result.success("修改密码成功！！");
            } else {
                throw new ProjectException("两次密码输入不一致");
            }
        }
        throw new ProjectException("旧密码错误！！");

    }
}
